package com.thb.pay.mq.core;

import com.thb.pay.mq.bean.BaseMessageEntity;
import com.thb.pay.mq.sender.MessageSender;
import com.thb.pay.mq.MQAccessBuilder;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.StringUtils;

/**
 * @Author: lucky
 * @Descrpiton:
 * @Date: 17:57 2018/8/31
 * @Modify by:
 */
@Slf4j
public abstract class AbsSender<T extends BaseMessageEntity> extends BaseAccessBuilder {


    private MessageSender messageSender;

    public void preInit() {
        try {
            if (connectionFactory() == null) {
                log.debug("connectionFactory not null");
                return;
            }
            MQAccessBuilder mqAccessBuilder = new MQAccessBuilder(connectionFactory(), getEnvironment());
            if (StringUtils.isEmpty(getExchange())) {
                log.debug("exchange not null");
                return;
            }
            messageSender = mqAccessBuilder.buildMessageSender(getExchange(), routing(), queue(), getType());
        } catch (Exception e) {
            log.error("pre init sender error.", e);
        }
    }


    public void send(T message) {
        messageSender.send(message);
    }
}
